package com.system.permissiondemo.controller;

import com.system.permissiondemo.common.RequestHolder;
import com.system.permissiondemo.common.ResultDTO;
import com.system.permissiondemo.dto.AclModuleLevelDTO;
import com.system.permissiondemo.param.AclModuleParam;
import com.system.permissiondemo.service.AclModuleService;
import com.system.permissiondemo.service.SysTreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @Description:    权限模版
* @Author:         Joe
* @CreateDate:     2020/3/27 14:17
*/
@RestController
@RequestMapping(value = "/sys/aclmodule")
@Api(tags = "权限模版管理")
@Slf4j
public class AclModuleController {

    @Autowired
    private AclModuleService moduleService;

    @Autowired
    private SysTreeService treeService;

    @ApiOperation(value = "添加权限模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "权限模版id",paramType = "query",dataType ="Integer"),
            @ApiImplicitParam(name = "name",value = "权限模版名称",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "parentId",value = "上一级权限模版id",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "seq",value = "权限模版在当前层级下的顺序（从小到大）",required = true,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "remark",value = "备注",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "status",value = "权限模版状态1，正常；0，冻结",required = true,paramType = "query",dataType ="Integer",defaultValue = "1")
    })
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResultDTO saveAclModule(AclModuleParam param){
        if (RequestHolder.getCurrentUser()==null){
            return ResultDTO.errorOfLogin();
        }
        moduleService.save(param);
        return ResultDTO.okOf();
    }

    @ApiOperation(value = "更新/编辑权限模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "权限模版id",paramType = "query",dataType ="Integer"),
            @ApiImplicitParam(name = "name",value = "权限模版名称",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "parentId",value = "上一级权限模版id",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "seq",value = "权限模版在当前层级下的顺序（从小到大）",required = true,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "remark",value = "备注",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "status",value = "权限模版状态1，正常；0，冻结",required = true,paramType = "query",dataType ="Integer",defaultValue = "1")
    })
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ResultDTO updateAclModule(AclModuleParam param){
        if (RequestHolder.getCurrentUser()==null){
            return ResultDTO.errorOfLogin();
        }
        moduleService.update(param);
        return ResultDTO.okOf();
    }

    @ApiOperation(value = "权限模版树结构")
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    public ResultDTO tree(){
        List<AclModuleLevelDTO> dtoList = treeService.aclModuleTree();
        return ResultDTO.okOf(dtoList);
    }
}
